
************************************************************************************************
* Tables to "Public Health Policy At Scale.."
************************************************************************************************

clear 
set more off 
set matsize 8000 
cap log close 

cd "D:\Data\workdata\707116\SIDS_RDD"

use "SIDS_analysis_final_full.dta", clear


log using "Revision1/log_files/05 Subgroups.log", replace

drop di*
g dif = birthday - mdy(12,31,1991)

gen     di1 =   dif 
gen     di1_i = dif 
replace di1_i = 0 if dif<0

g dc_sids_oth = dc_sids + dc_all_other 


g 		mor1_exc_sids = mor1 - dc_sids_oth
replace mor1_exc_sids =0 if mor1==0 


g 		mor5_exc_sids = mor5 - dc_sids_oth
replace mor5_exc_sids =0 if mor5==0 


g m_high_ed = m_short + m_mid + m_long
egen dc_other_non_sids = rowmax(dc_inf  dc_neo dc_nerv dc_resp)

g  preterm = . 
replace preterm = 1 if fullterm37==0
replace preterm = 0 if fullterm37==1

global motherset "mage m_basic m_voc m_high   m_immigrant" 
global childset "female border preterm LBW"
global outset1 "dc_sids dc_sids_oth mor1 mor5 mor1_exc_sids mor5_exc_sids"


label var mage "age at birth" 
label var m_basic "basic education" 
label var m_voc "vocational education" 
label var m_high "higher education" 
label var m_immigrant "immigrant"  

label var female "female" 
label var border "birth order" 
label var preterm " preterm"
label var LBW "low birth weight" 

label var dc_sids "SIDS mortality (over 10K)" 
label var dc_sids_oth "SIDS + all unclassified (over 10K)"
label var mor1 "infant mortality" 
label var mor5 "child mortality" 
label var mor1_exc_sids "infant mortality except SIDS" 
label var mor5_exc_sids "child mortality except SIDS" 



g   male = . 
replace male = 0 if female==1
replace male = 1 if female==0

g firstchild = . 
replace firstchild = 0 if border>1 
replace firstchild = 1 if border==1



global resset "LBW preterm male firstchild m_basic m_voc m_high m_immigrant"		


tempfile t1 
save `t1' 

eststo clear 



**Automated bw**
foreach x in $resset{
rdbwselect mor1 dif if `x'==1, bwselect(mserd)  
local bw = round(e(h_mserd))
reg mor1 treat di1* if inrange(dif,-`bw',`bw') & `x'==1, cluster(birthday)	
g N__`x'__`bw'__1 = e(N)
test treat 
g b__`x'__`bw'__1 = _b[treat]
g se__`x'__`bw'__1 = _se[treat]
g p__`x'__`bw'__1 = r(p)
qui sum mor1 if e(sample) & treat==0
g M__`x'__`bw'__1 = r(mean)
}

**static bw** 
foreach x in $resset{
foreach bw in 1000 1250 1500 1750 2000{
reg mor1 treat di1* if inrange(dif,-`bw',`bw') & `x'==1, cluster(birthday)	
g N__`x'__`bw'__2 = e(N)
test treat 
g b__`x'__`bw'__2 = _b[treat]
g se__`x'__`bw'__2 = _se[treat]
g p__`x'__`bw'__2 = r(p)
qui sum mor1 if e(sample) & treat==0
g M__`x'__`bw'__2 = r(mean)
}
} 
 
keep b__* se__* p__* N__* M__* 
keep if _n==1 
xpose, clear varname 
rename v1 v_scalar 
split _varname, p("__")
drop _varname 
rename _varname1 coef_name 
rename _varname2 vname 
rename _varname3 bw 
rename _varname4 col_index  
destring bw, replace 
destring col_index, replace 

g    		group = . 

foreach x in $cset1 $cset2{
replace group = 1 if vname =="`x'" 	
}

foreach x in $outset1{
replace group = 2 if vname =="`x'"
}

order coef_name group vname bw col_index v_scalar 

export excel using "Revision1\output\days\DSubsetRegTables.xlsx", firstrow(variables) replace 
 



log close 


